package com.hgy.hhao;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author HhaoAn
 * @date 2025年03月03日 19:33
 */
@Configuration
public class CommonConfig {
    /*
     *
     * @author HhaoAn
     * @date 2025/3/3 19:35
     * @return com.hgy.hhao.Test
     * 默认是方法名字:configTest
     */
    @Bean("自定义")//自定义Bean名字
    /*
     * 注册条件@Conditional：条件判断，满足条件，注册Bean
     * @ConditionalOnProperty：当配置文件中有这个属性值，才会注册这个Bean
     * @ConditionalOnMissingBean：当IOC容器中没有这个Bean（没有注入这个类），才会注册这个Bean
     * @ConditionalOnClass：当IOC容器中有这个Bean（类），才会注册这个Bean
     */
    @ConditionalOnProperty(prefix = "test",name = {"name","num"})//只有配置中都有配置name和num才会注册这个Bean
    public Test configTest(@Value("${test.name}") String name, @Value("${test.num}") String num) {
        Test test = new Test();
        test.setName(name);
        test.setNum(Integer.parseInt(num));
        return test;
    }
}

class Test {
    private String name;
    private int num;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return "在Config中配置Bean注释测试：Test{" +
                "name='" + name + '\'' +
                ", num=" + num +
                '}';
    }
}